package luanluan.control.list 
{
	import com.greensock.TweenLite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import luanluan.control.application.ObjectPool;
	import luanluan.control.buttons.LButton;
	import luanluan.data.LData;
	import luanluan.utils.LuanUtils;
	
	/**
	 * 翻页的列表
	 * @author luanluan
	 */
	public class LPageList implements IList 
	{
		//	*换页效果模式
		static public const CROSSWISE:String = "pageList.changePage.mode.crosswise";	//横向
		static public const NONE:String = "pageList.changePage.mode.none";				//无
		static public const UPRIGHT:String = "pageList.changePage.mode.upright";		//纵向
		
		
		//	*放置item的显示容器
		private var _container:DisplayObjectContainer;
		
		//	*下一页按钮
		private var _nextPageBtn:LButton;
		//	*上一页按钮
		private var _prevPageBtn:LButton;
		
		//	*item类名
		private var _itemClass:Class;
		//	*item列表
		private var _itemList:Vector.<IListItem>;
		
		//	*每页显示项目数量
		private var _pageCount:int;
		//	*总页数
		private var _maxPage:int;
		//	*当前显示页数
		private var _curPage:int;
		
		//	*数据列表
		private var _dataList:Vector.<LData>;
		
		//	*是否启用
		private var _enable:Boolean;
		
		//	*换页效果
		private var _changePageMode:String = NONE;
		private var _tempContainer:Sprite;
		private var _mask:Shape;
		
		
		/**
		 * 一个可以翻页的列表
		 * @param	container	放置item的容器
		 * @param	nextPageBtn	下一页按钮
		 * @param	prevPageBtn	上一页按钮
		 * @param	itemClass	item类名,实现IListItem接口
		 * @param	pageCount	每页显示数量
		 */
		public function LPageList(container:DisplayObjectContainer , nextPageBtn:MovieClip , prevPageBtn:MovieClip , itemClass:Class , pageCount:int = 10) 
		{
			_container = new Sprite();
			container.addChild(_container);
			
			_nextPageBtn = new LButton(nextPageBtn, true);
			_prevPageBtn = new LButton(prevPageBtn , true);
			_nextPageBtn.onMouseClick = _prevPageBtn.onMouseClick = changePageHandler;
			
			_itemClass = itemClass;
			
			_pageCount = pageCount;
			
			_mask = new Shape();
			_mask.graphics.beginFill(0);
			_mask.graphics.drawRect(0, 0, 1, 1);
			_mask.graphics.endFill();
			container.addChild(_mask);
			_container.mask = _mask;
		}
		
		//	*点击换页按钮
		private function changePageHandler(e:MouseEvent):void
		{
			if (e.currentTarget == _nextPageBtn.mc)
			{
				//下一页
				if (_curPage < _maxPage)
					refresh(1);
			}
			if (e.currentTarget == _prevPageBtn.mc)
			{
				//上一页
				if (_curPage > 1)
					refresh(-1);
			}
		}
		
		//	*数据改变是需要刷新才显示正确结果
		private function refresh(changeValue:int = 0):void
		{
			if (changeValue && _changePageMode != NONE)
				setTempView();
				
			clearItems();
			
			_curPage += changeValue;
			var len:int = _pageCount;
			if (_dataList.length < _pageCount * _curPage)
				len = _dataList.length - _pageCount * (_curPage-1);
				
			_itemList = new Vector.<IListItem>(len, true);
			for (var i:int = 0; i < len; i++) 
			{
				var item:IListItem = ObjectPool.instance.getObject(_itemClass) as IListItem;
				item.setData(_dataList[(_curPage - 1) * _pageCount + i]);
				_container.addChild(item.view);
				item.view.y = i * item.view.height;
				_itemList[i] = item;
			}
			
			_mask.width = item.view.width;
			_mask.height = item.view.height * _pageCount;
			
			_nextPageBtn.enable = _curPage < _maxPage;
			_prevPageBtn.enable = _curPage > 1;
			
			if (changeValue && _changePageMode != NONE)
				showChangePageEff(changeValue);
		}
		
		/**
		 * 设置临时视图
		 */
		private function setTempView():void
		{
			if (!_tempContainer)
				_tempContainer = new Sprite();
			for (var i:int = 0; i < _itemList.length; i++) 
			{
				_tempContainer.addChild(_itemList[i].view);
				_itemList[i].view.y = i * _itemList[i].view.height;
			}
		}
		
		/**
		 * 显示换页效果
		 * @param	changeValue
		 */
		private function showChangePageEff(changeValue:int):void 
		{
			_container.addChild(_tempContainer);
			var dis:Number;
			if (_changePageMode == CROSSWISE)
			{
				if (changeValue > 0)
					dis = _mask.width;
				else
					dis = -_mask.width;
				_tempContainer.x = -1 * dis;
				_container.x = dis;
				TweenLite.to(_container, .3, { x:0, onComplete:changeEffFinish } );
			}
			else
			{
				if (changeValue > 0)
					dis = _mask.height;
				else
					dis = -_mask.height;
				_tempContainer.y = -1 * dis;
				_container.y = dis;
				TweenLite.to(_container, .3, { y:0, onComplete:changeEffFinish } );
			}
		}
		
		//	*换页效果结束
		private function changeEffFinish():void
		{
			_container.removeChild(_tempContainer);
			LuanUtils.clearContainer(_tempContainer);
		}
		
		//	*清除列表项
		private function clearItems():void
		{
			LuanUtils.clearContainer(_container);
			
			var len:int = _itemList ? _itemList.length : 0;
			for (var i:int = 0; i < len; i++) 
			{
				_itemList[i].clear();
				ObjectPool.instance.push(_itemList[i]);
			}
			_itemList = null;
		}
		
		/* INTERFACE luanluan.control.list.IList */
		
		/**
		 * 设置数据(会清空数据,并显示第一页)
		 * @param	dataList
		 */
		public function setDataList(dataList:Vector.<LData>):void 
		{
			clearAllData();
			_dataList = dataList;
			_curPage = 1;
			_maxPage = Math.ceil(_dataList.length / _pageCount);
			refresh();
		}
		
		/**
		 * 添加数据
		 * @param	data
		 */
		public function addData(data:LData):void 
		{
			_dataList.push(data);
			if (_curPage == _maxPage)
				refresh();
		}
		
		/**
		 * 清除所有数据(包括显示)
		 */
		public function clearAllData():void
		{
			clearItems();
			_dataList = null;
		}
		
		/**
		 * 设置启用
		 */
		public function set enable(value:Boolean):void 
		{
			_nextPageBtn.enable = _prevPageBtn.enable = _enable = value;
			if (_enable)
			{
				_nextPageBtn.enable = _curPage < _maxPage;
				_prevPageBtn.enable = _curPage > 1;
			}
		}
		public function get enable():Boolean 	{	return _enable;	}
		
		/**
		 * 设置换页模式 请使用LPageList静态常量
		 */
		public function set changePageMode(value:String):void 
		{
			_changePageMode = value;
		}
		
	}

}